// components/store/goodsCell/index.js
import {
  throttle
} from '../../../utils/vutils'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
      observer: function (newVal, oldVal, changedPath) {
        let goodsLabel = newVal.goodsLabel
        let tagArr = []
        goodsLabel.forEach((item) => {
          for (var key in item) {
            tagArr.push(item[key])
          }
        })
        this.setData({
          tagList: tagArr
        })
        //设置分享金额颜色
        let map = {
          '0.03': '03', //'#a8d091',
          '0.05': '05', //'#528139',
          '0.07': '07', //'#9dc4eb',
          '0.1': '10', //'#1c517d',
          '0.10': '10', //'#1c517d',
          '0.15': '15', //'#ffc000',
          '0.20': '20', //'#be0002',
          '0.2': '20', //'#be0002',
        }
        if (newVal.hasOwnProperty('commission2') && map.hasOwnProperty(newVal.commission2)) {
          this.setData({
            colorCls: 'label-color label-' + map[newVal.commission2]
          })
        } else {
          this.setData({
            colorCls: ''
          })
        }
      }
    },
    cooperationType: {
      type: [Number, String]
    },
    isSeckillCategroy: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    colorCls: '',
    tagList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClick() {
      const {
        goodsId,
        storeId
      } = this.data.info
      const {
        colorCls
      } = this.data
      wx.navigateTo({
        url: `/pages/goods/mmallGoodsDetails/mmallGoodsDetails?id=${goodsId}&storeId=${storeId}&colorCls=${colorCls}`
      })
    },
    // 加入购物车
    onAddCart: throttle(function (event) {
      const {
        goodsStatus
      } = this.data.info
      // 已售完 或 打烊
      if (goodsStatus !== 1) {
        return
      }
      this.triggerEvent('addCart', {
        goods: this.data.info,
        pos: {
          x: event.touches[0].clientX,
          y: event.touches[0].clientY
        }
      })
    }, 500, {
      immediate: true
    }),
    // 选择规格
    onSelectSku(e) {
      // 已售完 或 打烊
      const {
        goodsStatus
      } = this.data.info
      if (goodsStatus !== 1) {
        return
      }
      this.triggerEvent('selectSku', {
        goods: this.data.info
      })
    }
  }
})